﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCM.UI.ChildWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCM.UI.Utils;
using Microsoft.Practices.Unity;
using Baosight.Platform.Core.ServiceModel;
using Baosight.Platform.Core.UI;

namespace Baosight.ZNH.ZMCM.UI
{
    public partial class ZMCMF3 : EFViewCmes
    {
        public ZMCMF3()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 列表中修改、删除前设置属性、参数
        /// 传递到后台代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.Grid.GetCheckedEIRows();
            _view.Args.Add("fdReceiveId", this.fdReceiveId.Value.ToString());//外键赋值
            _view.Args.Add("fdIsfromPlan", this.fdIsfromPlan.Value.ToString());//外键赋值

            this.ViewProperty = _view;
        }

        /// 点击新增按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            TempButton = sender as Button;
            TempButton.IsEnabled = false;
            CmesWindows _popup = new CmesWindows();//构造一个弹出页面

            if (this.fdIsfromPlan.Value.Equals("1"))
            {
                BatchRegister view = new BatchRegister("insert");//构造一个新的修改页面
                EIInfo eiInfo = new EIInfo();
                view.EIDataContext.Update(eiInfo);
                view.fdReceiveId.Value = fdReceiveId.Value;
                view.fdIsfromPlan.Value = "1";
                _popup.Header = "到货登记明细-新增";//设置Title
                _popup.Content = view;//让新增页面在弹出窗口显示
            }
            else
            {
                EIInfo eiInfo = new EIInfo();
                eiInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.units"));
                ArrivalRegisterModify view = new ArrivalRegisterModify("insert");//构造一个新的修改页面
                view.EIDataContext = new Platform.Core.UI.EIDataContext();
                view.EIDataContext.Update(eiInfo);

                view.fdIsfromPlan.Value = this.fdIsfromPlan.Value;
                ((view.fdMaterialCode.Content) as CmesCustomView).eViewInitParams = "bindingService=ZMCMF1.materialcode,columns=fdKeyId|fdYear|fdMonth|fdMaterialCode|fdMaterialName|fdQuality|fdSpecModel|fdNumber|fdPrice|fdSum|fdRecevieSum|fdMetricName,queryConditions=fdMaterialCode|fdMaterialName|fdSpecModel,initParam=fdIsfromPlan:0";
                view.fdReceiveId.Value = fdReceiveId.Value;
                view.fdNotReceiveNum.Visibility = Visibility.Collapsed;

                //用于区别同一物资编码的值变化
                view.fdStockPlanDetailId.eBindingName = "result-0-fdMaterialCode";
                view.fdMaterialCode.eBindingName = "result-0-fdStockPlanDetailId";
                ((view.fdMaterialCode.Content) as CmesCustomView).eValueMemberPath = "fdKeyId";

                _popup.Header = "到货登记明细-新增";//设置Title
                _popup.Content = view;//让新增页面在弹出窗口显示
            }

            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(OpenInsertWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 新增弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenInsertWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新从表Grid区域
                this.Grid.Refresh();
                this.UpdateHomePageData(new EIInfo());
            }

            //按钮生效
            base.TempButtonOpen(true);

            if (!this.fdIsfromPlan.Value.Equals("1"))
            {
                ArrivalRegisterModify view = (sender as EFWindow).Content as ArrivalRegisterModify;
                if (!view.InsertMeg.Equals(""))
                {
                    this.OpenEFWindowAlert(view.InsertMeg, true);
                }
            }
        }

        /// <summary>
        /// 从表修改操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;

            if (BeforeAction != null)
            {
                BeforeAction.Invoke(null, null);
            }

            int count = this.ViewProperty.EFGridCheckedRows.Count;
            if (count <= 0)
            {
                this.OpenEFWindowAlert("请选择一条记录进行修改！", true);
                return;
            }
            else if (count > 1)
            {
                this.OpenEFWindowAlert("最多只能选择一条记录进行修改！", true);
                return;
            }
            else
            {
                if (BeforeModify(this.ViewProperty.EFGridCheckedRows))
                {
                    if (null == this.ViewProperty.EFGridCheckedRows[0]["fdMaterialCode"]
                            || "".Equals(this.ViewProperty.EFGridCheckedRows[0]["fdMaterialCode"]))
                    {
                        update();
                    }
                    else
                    {
                        EIInfo eiInfo = new EIInfo();
                        eiInfo.Set("service", "ZMCMF1");
                        eiInfo.Set("method", "validateUpdate");
                        eiInfo.Set("fdMaterialCode", this.ViewProperty.EFGridCheckedRows[0]["fdMaterialCode"]);

                        HttpServiceAgent agent = new HttpServiceAgent();
                        agent.CallService(eiInfo, OnDoWorkCompletedUpdated);
                    }
                }
            }
        }

        private void OnDoWorkCompletedUpdated(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                update();
            }
        }

        private void update()
        {
            IList<EIRow> _list = this.Grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.Grid.GetCheckedEIRows();

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);

            OpenModifyPopup(eiInfo, "到货登记管理-修改", "update", null);//构造弹出新增页面
        }

        /// <summary>
        /// 修改前的判断
        /// </summary>
        /// <param name="_eIRows"></param>
        /// <returns></returns>
        private bool BeforeModify(List<EIRow> _eIRows) 
        {
            if ("验收".Equals(_eIRows[0]["fdValidateStatus"]))
            {
                base.OpenEFWindowAlert("已验收，不能修改！", true);
                return false;
            }
            else if (_eIRows[0]["fdImportnoteCode"] != null && !"".Equals(_eIRows[0]["fdImportnoteCode"]))
            {
                base.OpenEFWindowAlert("已办理入库，不能修改！", true);
                return false;
            }

            return true;
        }

        /// <summary>
        /// 删除操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnDeleteButtonClick(object sender, RoutedEventArgs e)
        {
            //按钮失效
            Button _button = sender as Button;
            _button.IsEnabled = false;
            base.TempButton = _button;
            int count = this.Grid.GetCheckedEIRows().Count;
            if (count <= 0)
            {
                this.OpenEFWindowAlert("请选择一条或多条记录进行删除！", true);
                return;
            }

            IList<EIRow> _list = this.Grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.Grid.GetCheckedEIRows();
            for (int i = 0; i < _list.Count; i++)
            {
                //if (!this.Grid.GetCheckedEIRows()[i]["fdDetailId"].ToString().Equals("no"))
                //{
                //    base.OpenEFWindowAlert("选中的记录中["
                //                            + this.Grid.GetCheckedEIRows()[i]["fdMaterialName"].ToString() 
                //                            + "]来自采购计划，不可删除！", true);
                //    return;
                //}
                if (!this.Grid.GetCheckedEIRows()[i]["fdValidateStatus"].ToString().Equals("到货"))
                {
                    base.OpenEFWindowAlert("选中的记录中包含已验收或入库的记录，不可删除！", true);
                    return;
                }
            }

            base.OnDeleteButtonClick("ZMCMF1", "delete", sender, e);
        }

        /// <summary>
        /// 从表验收操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnAcceptanceButtonClick(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;

            if (BeforeAction != null)
            {
                BeforeAction.Invoke(null, null);
            }

            int count = this.ViewProperty.EFGridCheckedRows.Count;
            if (count <= 0)
            {
                this.OpenEFWindowAlert("请选择一条记录进行验收！", true);
                return;
            }
            else if (count > 1)
            {
                this.OpenEFWindowAlert("最多只能选择一条记录进行验收！", true);
                return;
            }
            else
            {
                if (BeforeAcceptance(this.ViewProperty.EFGridCheckedRows))
                {
                    if (null == this.ViewProperty.EFGridCheckedRows[0]["fdMaterialCode"]
                            || "".Equals(this.ViewProperty.EFGridCheckedRows[0]["fdMaterialCode"]))
                    {
                        acceptance();
                    }
                    else
                    {
                        EIInfo eiInfo = new EIInfo();
                        eiInfo.Set("service", "ZMCMF1");
                        eiInfo.Set("method", "validateCheck");
                        eiInfo.Set("fdMaterialCode", this.ViewProperty.EFGridCheckedRows[0]["fdMaterialCode"]);

                        HttpServiceAgent agent = new HttpServiceAgent();
                        agent.CallService(eiInfo, OnDoWorkCompletedAcceptance);
                    }
                }
            }

        }

        private void OnDoWorkCompletedAcceptance(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                acceptance();
            }
        }

        /// <summary>
        /// 验收前的判断
        /// </summary>
        /// <param name="_eIRows"></param>
        /// <returns></returns>
        private bool BeforeAcceptance(List<EIRow> _eIRows)
        {
            if (!"到货".Equals(_eIRows[0]["fdValidateStatus"]) || (_eIRows[0]["fdImportnoteCode"] != null && !"".Equals(_eIRows[0]["fdImportnoteCode"])))
            {
                base.OpenEFWindowAlert("该到货明细不可再次验收！", true);
                return false;
            }
           
            return true;
        }

        private void acceptance()
        {
            IList<EIRow> _list = this.Grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.Grid.GetCheckedEIRows();

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);

            OpenAcceptancePopup(eiInfo, "到货登记管理-验收", "update", null);//构造弹出新增页面
        }

        /// <summary>
        /// 从表办理入库操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnStorageButtonClick(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;

            if (BeforeAction != null)
            {
                BeforeAction.Invoke(null, null);
            }

            int count = this.ViewProperty.EFGridCheckedRows.Count;
            if (count <= 0)
            {
                this.OpenEFWindowAlert("请选择一条记录进行办理入库！", true);
                return;
            }
            else if (count > 1)
            {
                this.OpenEFWindowAlert("最多只能选择一条记录进行办理入库！", true);
                return;
            }
            else
            {
                if (BeforeStorage(this.ViewProperty.EFGridCheckedRows))
                {
                    if (null == this.ViewProperty.EFGridCheckedRows[0]["fdMaterialCode"]
                            || "".Equals(this.ViewProperty.EFGridCheckedRows[0]["fdMaterialCode"]))
                    {
                        EIInfo eiInfo = new EIInfo();
                        eiInfo.Set("service", "ZMCMF3");
                        eiInfo.Set("method", "queryHasImportNoCodeMaterial");
                        eiInfo.Set("fdRequirePlan", this.ViewProperty.EFGridCheckedRows[0]["fdRequirePlan"]);

                        HttpServiceAgent agent = new HttpServiceAgent();
                        agent.CallService(eiInfo, OnDoWorkCompletedValidateTypeManagement);
                    }
                    else
                    {
                        EIInfo eiInfo = new EIInfo();
                        eiInfo.Set("service", "ZMCMF1");
                        eiInfo.Set("method", "validateImport");
                        eiInfo.Set("fdMaterialCode", this.ViewProperty.EFGridCheckedRows[0]["fdMaterialCode"]);

                        HttpServiceAgent agent = new HttpServiceAgent();
                        agent.CallService(eiInfo, OnDoWorkCompletedStoraged);
                    }
                }
            }
        }

        private void OnDoWorkCompletedValidateTypeManagement(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.GetBlocks().ContainsKey("queryResult"))
            {
                EIInfo eiInfo = new EIInfo();
                eiInfo.Set("service", "ZMCMF1");
                eiInfo.Set("method", "validateImport");
                eiInfo.Set("fdMaterialCode", e.EIInfo.GetBlock("queryResult").GetRow(0)["fdMaterialCode"]);
                eiInfo.AddBlock(e.EIInfo.GetBlock("queryResult"));

                HttpServiceAgent agent = new HttpServiceAgent();
                agent.CallService(eiInfo, OnDoWorkCompletedStoraged);
            }
            else
            {
                if (e.EIInfo.Status == -1)
                {
                    this.TempButtonOpen(true);
                    this.OnCommitMessagePopup(e.EIInfo);//操作失败，消息提醒
                }
                else
                {
                    storage(e.EIInfo);
                }
            }
        }

        private void OnDoWorkCompletedStoraged(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                this.OnCommitMessagePopup(inInfo);//操作失败，消息提醒
            }
            else
            {
                if (e.EIInfo.GetBlocks().ContainsKey("queryResult"))
                {
                    storage(e.EIInfo);
                }
                else
                {
                    EIInfo info = new EIInfo();
                    info.Set("fdNoteCode", e.EIInfo.Get("fdNoteCode"));
                    storage(info);
                }
            }
        }

        private void storage(EIInfo info)
        {
            IList<EIRow> _list = this.Grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.Grid.GetCheckedEIRows();

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);
            eiInfo.Set("fdNoteCode", info.Get("fdNoteCode"));

            if (row["fdMaterialCode"] != null)
            {
                if (!row["fdMaterialCode"].ToString().Equals(""))
                {
                    OpenStoragePopup(eiInfo, "到货登记管理-办理入库", "insert", null);
                }
            }
            else
            {
                if (info.GetBlocks().ContainsKey("queryResult"))
                {
                    eiInfo.AddBlock(info.GetBlock("queryResult"));
                }
                OpenStoragePopup1(eiInfo, "到货登记管理-新增物资资料", "insert", null);
            }
        }

        /// <summary>
        /// 入库前的判断
        /// </summary>
        /// <param name="_eIRows"></param>
        /// <returns></returns>
        private bool BeforeStorage(List<EIRow> _eIRows)
        {
            if (!"验收".Equals(_eIRows[0]["fdValidateStatus"]))
            {
                base.OpenEFWindowAlert("该笔到货还未验收，不能入库！", true);
                return false;
            }
            else if (_eIRows[0]["fdImportnoteCode"] != null && !"".Equals(_eIRows[0]["fdImportnoteCode"]))
            {
                base.OpenEFWindowAlert("该笔到货已办理入库，不能重复入库！", true);
                return false;
            }

            return true;
        }

        /// <summary>
        /// 从表弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == true)
            {
                //刷新从表Grid区域
                this.Grid.Refresh();
                UpdateHomePageData(new EIInfo());
            }

            //按钮生效
            base.TempButtonOpen(true);

            //超链接生效
            if (base.TempHyperlink != null)
            {
                base.TempHyperlink.IsEnabled = true;
                base.TempHyperlink = null;
            }
        }



        /// <summary>
        /// 从表修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        private void OpenModifyPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            ArrivalRegisterModify modify = new ArrivalRegisterModify(_dialogType);//构造一个新的修改页面
            modify.EIDataContext = new Platform.Core.UI.EIDataContext();
            modify.EIDataContext.Update(_eIInfo);
            if ("update".Equals(_dialogType))
            {
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.units"));
                if (!_eIInfo.GetBlock("result").Get(0, "fdDetailId").Equals("no"))//如果入库物资来自计划则应收数量不能修改
                {
                    modify.fdPlanNum.IsReadOnly = true;
                }
            }

            modify.fdNotReceiveNum.Visibility = Visibility.Collapsed;
            if (this.fdIsfromPlan.Value.Equals("1"))
            {
                modify.fdActualNum.IsReadOnly = true;
                modify.fdReceiveSum.IsReadOnly = true;
            }
            modify.fdIsfromPlan.Value = this.fdIsfromPlan.Value;
            double fdNotNumber = 0;
            double fdActualNum = 0;
            Double.TryParse(this.Grid.CheckedEIRows[0]["fdNotNumber"].ToString(),out fdNotNumber) ;
            Double.TryParse(this.Grid.CheckedEIRows[0]["fdActualNum"].ToString(), out fdActualNum);

            modify.fdNotReceiveNum.Value = fdNotNumber + fdActualNum;
            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = modify;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(OpenWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 从表验收弹出窗口
        /// </summary>
        /// <param name="_eIInfo"></param>
        /// <param name="_title"></param>
        /// <param name="_dialogType"></param>
        /// <param name="_args"></param>
        private void OpenAcceptancePopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            ArrivalRegisterAcceptance acceptance = new ArrivalRegisterAcceptance(_dialogType);//构造一个新的修改页面
            acceptance.EIDataContext = new Platform.Core.UI.EIDataContext();

            if ("update".Equals(_dialogType))
            {
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.units"));
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.constructionStaff"));
                acceptance.fdValidatePeople.Value = this.EIDataContext.GetAttribute("validatePeople");
            }
            acceptance.EIDataContext.Update(_eIInfo);

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = acceptance;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(OpenWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 从表办理入库弹出窗口
        /// </summary>
        /// <param name="_eIInfo"></param>
        /// <param name="_title"></param>
        /// <param name="_dialogType"></param>
        /// <param name="_args"></param>
        private void OpenStoragePopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            ArrivalRegisterStorage storage = new ArrivalRegisterStorage(_dialogType);//构造一个新的修改页面
            storage.EIDataContext = new Platform.Core.UI.EIDataContext();

            if ("insert".Equals(_dialogType))
            {
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.units"));
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.constructionStaff"));
                _eIInfo.AddBlock(new StorageBlock().GetImportTypeBlock(false));
                _eIInfo.AddBlock(new StorageBlock().GetMaterialSourceBlock(false));

                storage.fdNoteDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
                storage.fdDepotName.Value = this.EIDataContext.GetAttribute("depotName");
                storage.fdNoteCode.Value = _eIInfo.Get("fdNoteCode");
                (storage.fdConfirmPeople.Content as CmesCustomView).Value = this.EIDataContext.GetAttribute("userId");
                (storage.fdConfirmPeople.Content as CmesCustomView).Display = this.EIDataContext.GetAttribute("userName");

                EIRow row = _eIInfo.GetBlock("result").GetRow(0);
                storage.fdMetric.Value = row["fdMetric"];
                storage.fdMaterialName.Value = row["fdMaterialName"];
                storage.fdPrice.Value = (double.Parse(row["fdValidateSum"].ToString()) / double.Parse(row["fdValidateNum"].ToString())).ToString("0.00000");
                storage.fdActualNumber.Value = row["fdValidateNum"];
                storage.fdPlanNumber.Value = row["fdValidateNum"];
                storage.fdKeyId.Value = row["fdKeyId"];
            }
            storage.EIDataContext.Update(_eIInfo);

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = storage;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(OpenWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 从表先新增物资，后办理入库弹出窗口
        /// </summary>
        /// <param name="_eIInfo"></param>
        /// <param name="_title"></param>
        /// <param name="_dialogType"></param>
        /// <param name="_args"></param>
        private void OpenStoragePopup1(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            ArrivalRegisterStorage1 storage = new ArrivalRegisterStorage1();//构造一个新的修改页面
            storage.EIDataContext = new Platform.Core.UI.EIDataContext();

            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.units"));
            _eIInfo.AddBlock((new BaseBlock()).GetYesOrNo());
            _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cs.constructionStaff"));
            _eIInfo.AddBlock(new StorageBlock().GetImportTypeBlock(false));
            _eIInfo.AddBlock(new StorageBlock().GetMaterialSourceBlock(false));

            storage.fdNoteDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
            storage.fdDepotName.Value = this.EIDataContext.GetAttribute("depotName");
            storage.fdNoteCode.Value = _eIInfo.Get("fdNoteCode");
            (storage.fdConfirmPeople.Content as CmesCustomView).Value = this.EIDataContext.GetAttribute("userId");
            (storage.fdConfirmPeople.Content as CmesCustomView).Display = this.EIDataContext.GetAttribute("userName");

            EIRow row = _eIInfo.GetBlock("result").GetRow(0);
            storage.material_fdMetric.Value = row["fdMetric"];
            storage.material_fdSpecModel.Value = row["fdSpecModel"];
            storage.material_fdQuality.Value = row["fdQuality"];
            storage.material_fdNumber.Value = row["fdNumber"];
            storage.material_fdMaterialName.Value = row["fdMaterialName"];
            storage.material_fdPrice.Value = (double.Parse(row["fdValidateSum"].ToString()) / double.Parse(row["fdValidateNum"].ToString())).ToString("0.00000");
            storage.material_fdOldKeyId.Value = row["fdKeyId"];
            storage.material_fdRequirePlan.Value = row["fdRequirePlan"];
            storage.fdActualNumber.Value = row["fdValidateNum"];
            storage.fdPlanNumber.Value = row["fdValidateNum"];

            if (_eIInfo.GetBlocks().ContainsKey("queryResult"))
            {

                (storage.material_fdNewMaterial.Content as EFComboBox).SelectedIndex = 1;
                storage.material_fdMaterialTypeInnerCode.Value = _eIInfo.GetBlock("queryResult").GetRow(0)["fdTypeCode"];
                storage.material_fdTypeCode.Value = _eIInfo.GetBlock("queryResult").GetRow(0)["fdTypeAllCode"];

                storage.material_fdMaterialName1.Value = _eIInfo.GetBlock("queryResult").GetRow(0)["fdMaterialName"];
                (storage.material_fdMaterialName1.Content as CmesCustomView).Display = _eIInfo.GetBlock("queryResult").GetRow(0)["fdMaterialName"];
                storage.material_fdMaterialId.Value = _eIInfo.GetBlock("queryResult").GetRow(0)["fdKeyId"];
                storage.material_fdMaterialCode.Value = _eIInfo.GetBlock("queryResult").GetRow(0)["fdMaterialCode"];
                storage.material_fdMaterialInnerCode.Value = _eIInfo.GetBlock("queryResult").GetRow(0)["fdMaterialInnerCode"];
                storage.material_fdMaterialLastCode.Value = _eIInfo.GetBlock("queryResult").GetRow(0)["fdMaterialLastCode"];
                storage.material_fdMaterialType.Value =  _eIInfo.GetBlock("queryResult").GetRow(0)["fdMaterialType"];
                storage.material_fdMaterialTypeId.Value = _eIInfo.GetBlock("queryResult").GetRow(0)["fdMaterialTypeId"];
                (storage.material_fdMaterialTypeId.Content as CmesCustomView).Display = _eIInfo.GetBlock("queryResult").GetRow(0)["fdMaterialType"];
                storage.material_fdQuality.Value = _eIInfo.GetBlock("queryResult").GetRow(0)["fdQuality"];
                storage.material_fdSpecModel.Value = _eIInfo.GetBlock("queryResult").GetRow(0)["fdSpecModel"];
                storage.material_fdNumber.Value = _eIInfo.GetBlock("queryResult").GetRow(0)["fdNumber"];
                storage.material_fdPrice.Value = _eIInfo.GetBlock("queryResult").GetRow(0)["fdPrice"];
                storage.material_fdMetric.Value = _eIInfo.GetBlock("queryResult").GetRow(0)["fdMetric"];
                storage.material_fdMatchDevice.Value = _eIInfo.GetBlock("queryResult").GetRow(0)["fdMatchDevice"];
                storage.material_fdPurchasePeriod.Value = _eIInfo.GetBlock("queryResult").GetRow(0)["fdPurchasePeriod"];
                storage.material_fdWeight.Value = _eIInfo.GetBlock("queryResult").GetRow(0)["fdWeight"];
                storage.material_fdSize.Value = _eIInfo.GetBlock("queryResult").GetRow(0)["fdSize"];
                storage.material_fdParameter.Value = _eIInfo.GetBlock("queryResult").GetRow(0)["fdParameter"];
                storage.material_fdFactory.Value = _eIInfo.GetBlock("queryResult").GetRow(0)["fdFactory"];
                storage.material_fdSupplyName.Value = _eIInfo.GetBlock("queryResult").GetRow(0)["fdSupplyName"];
                storage.material_fdMaterialStatus.Value = _eIInfo.GetBlock("queryResult").GetRow(0)["fdMaterialStatus"];
                storage.material_fdRemark.Value = _eIInfo.GetBlock("queryResult").GetRow(0)["fdRemark"];
                BaseTools.EFDataFieldNotEnabled(storage.MaterialInsertGrid.GetChildObjects<EFDataField>(null, true));
            }
            else
            {
                (storage.material_fdNewMaterial.Content as EFComboBox).SelectedIndex = 0;
            }


            storage.EIDataContext.Update(_eIInfo);

            CmesWindows _popup = new CmesWindows();//构造一个弹出页面
            _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
            _popup.Header = _title == null ? "" : _title;//设置Title
            _popup.Content = storage;//让新增页面在弹出窗口显示
            _popup.Closed += new EventHandler<WindowClosedEventArgs>(OpenWindow_Closed);
            _popup.ShowDialog();//模态显示弹出窗口
        }

        /// <summary>
        /// 用于刷新主表页面
        /// </summary>
        public override void UpdateHomePageData(EIInfo data)
        {
            // 数据发布
            IEventAggregator eventAggreator = Container.Resolve<IEventAggregator>();
            EIDataPublishedEvent dataPublishedEvent = eventAggreator.GetEvent<EIDataPublishedEvent>();
            dataPublishedEvent.Publish(data);
        }
    }
}
